//
//  UILabel+labelTextContent.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/3/14.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "UILabel+labelTextContent.h"
#import "DGSAttributedStringSuite.h"
#import <CoreText/CoreText.h>
@implementation UILabel (labelTextContent)


- (NSArray *)getLinesArrayOfStringInLabel{
    
   
    NSString *text = [self text];
    if (text == nil) {
        return @[];
    }
    UIFont *font = [self font];
    CGRect rect = [self frame];
    
    CTFontRef myFont = CTFontCreateWithName(( CFStringRef)([font fontName]), [font pointSize], NULL);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge  id)myFont range:NSMakeRange(0, attStr.length)];
    CFRelease(myFont);
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( CFAttributedStringRef)attStr);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
    NSArray *lines = ( NSArray *)CTFrameGetLines(frame);
    NSMutableArray *linesArray = [[NSMutableArray alloc]init];
    for (id line in lines) {
        CTLineRef lineRef = (__bridge  CTLineRef )line;
        CFRange lineRange = CTLineGetStringRange(lineRef);
        NSRange range = NSMakeRange(lineRange.location, lineRange.length);
        NSString *lineString = [text substringWithRange:range];
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithFloat:0.0]));
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithInt:0.0]));
        //NSLog(@"''''''''''''''''''%@",lineString);
        [linesArray addObject:lineString];
    }
    
    CGPathRelease(path);
    CFRelease( frame );
    CFRelease(frameSetter);
    return (NSArray *)linesArray;
}


- (void)expendWithIsexpend:(BOOL)isExpend withDefaultNumberOfline:(NSInteger)number{


       NSArray *arr = [self getLinesArrayOfStringInLabel];
    
       if (arr.count<=number) {
          return;
       }
    
        NSString *title;
        
        NSInteger last_line_index ;
        if (isExpend) {
            title = @"[收起]";
            last_line_index = arr.count-1;
        }else{
            title = @"...[展开]";
            last_line_index = number;
        }
        
        
        
        
        NSMutableString *wholeStr =[NSMutableString string];
        [arr enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (!isExpend) {
                if (idx<last_line_index -1) {
                    [wholeStr appendString:obj];
                }else if (idx == last_line_index){  //最后一行肯定是占满
                    
                    if (obj.length >=10) {
                        [wholeStr appendString:[obj substringWithRange:NSMakeRange(0, obj.length - 10)]];
                    }else{
                        [wholeStr appendString:[obj substringWithRange:NSMakeRange(0, obj.length - 1)]];
                    }
                    
                }
            }else{
                [wholeStr appendString:obj];
            }
            
            
        }];
        
        NSMutableAttributedString *whole_attStr = [NSMutableAttributedString dgs_makeString:^(DGSAttributedStringMaker *add) {
            add.string(wholeStr).color(kTxtColor).fontSize(14);
            add.string(title).color(kPinchColor).fontSize(13);
            
        }];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
   
        [paragraphStyle setLineSpacing:6];
        [whole_attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, whole_attStr.length)];
        [whole_attStr addAttributes:@{NSKernAttributeName:@(1)} range:NSMakeRange(0, whole_attStr.length - title.length)];
//       [whole_attStr addAttributes:@{NSForegroundColorAttributeName:kPinchColor, NSLinkAttributeName:title } range:[whole_attStr.string rangeOfString:title]];
    
       self.attributedText = whole_attStr;
    
}


@end
